﻿using Abp.Application.Services;
using SportsPlatform.Base;
using SportsPlatform.Dto.SmsVerification;
using SportsPlatform.Helpers;
using SportsPlatform.Query.SmsVerification;
using SportsPlatform.SmsVerification.ISmsVerification;
using SportsPlatform.Consts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsPlatform.Configuration;
using Abp.Reflection.Extensions;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System.Web;
using Microsoft.AspNetCore.Http;

namespace SportsPlatform.SmsVerification.SmsVerification
{
    /// <summary>
    /// 短信服务实现
    /// </summary>
    [RemoteService(false)]
    public class SmsService : SportsPlatformAppServiceBase, ISmsService
    {
        //配置
        private readonly IConfiguration _appConfiguration;
        /// <summary>
        /// 
        /// </summary>
        public SmsService(IHttpContextAccessor context) : base(context)
        {
            _appConfiguration = AppConfigurations.Get(typeof(SmsService).GetAssembly().GetDirectoryPathOrNull());
        }
        /// <summary>
        /// 发送短信验证
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDto<SendMessageDto>> SendMessage(SendMessageQuery query)
        {
            ResultDto<SendMessageDto> resultDto = new ResultDto<SendMessageDto>();
            try
            {
                string url = _appConfiguration["Sms:SmsUrl"];
                string code = _appConfiguration["Sms:InterfaceCode"];
                string message = _appConfiguration["Sms:Message"];
                //生成随机数验证码
                byte[] buffer = Guid.NewGuid().ToByteArray();
                int iSeed = BitConverter.ToInt32(buffer, 0);
                Random random = new Random(iSeed);
                //随机数
                string number = random.Next(100000, 999999).ToString();

                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic["interfaceCode"] = code;
                dic["jkbs"] = "SendMes";
                dic["phone"] = query.Tel;
                dic["message"] = string.Format(message, number);
                string request = JsonConvert.SerializeObject(dic);
                //urlencode编码
                string par = HttpUtility.UrlEncode(request);
                string httpurl = string.Format(url + "?par={0}", par);
                var ret = await httpurl.PostAsync(request);
                resultDto.IsSuccess = (bool)ret["success"];
                resultDto.Code = ret["code"].ToString().Equals("0") ? HttpResultCode.Success : HttpResultCode.Fail;
                resultDto.Message = ret["msg"].ToString();
                resultDto.Data = resultDto.Code == HttpResultCode.Success ? (new SendMessageDto() { VerificationCode = number }) : null;
            }
            catch (Exception ex)
            {
                resultDto.IsSuccess = false;
                resultDto.Code = HttpResultCode.Error;
                resultDto.Message = ex.Message.ToString();
            }
            return resultDto;
        }
    }
}
